package com.EAsystem.service.Impl;

import com.EAsystem.entity.HomeworkSubmission;
import com.EAsystem.mapper.HomeworkSubmissionMapper;
import com.EAsystem.service.HomeworkSubmissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 作业提交管理Service实现类
 */
@Service
public class HomeworkSubmissionServiceImpl implements HomeworkSubmissionService {

    @Autowired
    private HomeworkSubmissionMapper homeworkSubmissionMapper;

    @Override
    public boolean addHomeworkSubmission(HomeworkSubmission homeworkSubmission) {
        // 设置初始分数为null
        homeworkSubmission.setScore(null);
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        homeworkSubmission.setCreateTime(now);
        homeworkSubmission.setUpdateTime(now);
        return homeworkSubmissionMapper.insertHomeworkSubmission(homeworkSubmission) > 0;
    }

    @Override
    public boolean updateHomeworkSubmission(HomeworkSubmission homeworkSubmission) {
        // 设置更新时间
        homeworkSubmission.setUpdateTime(LocalDateTime.now());
        return homeworkSubmissionMapper.updateHomeworkSubmission(homeworkSubmission) > 0;
    }

    @Override
    public boolean deleteHomeworkSubmission(Long id) {
        try {
            return homeworkSubmissionMapper.deleteHomeworkSubmissionById(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public HomeworkSubmission getHomeworkSubmissionById(Long id) {
        try {
            return homeworkSubmissionMapper.findHomeworkSubmissionById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



    @Override
    public List<HomeworkSubmission> getHomeworkSubmissionsByHomeworkId(Long homeworkId) {
        try {
            return homeworkSubmissionMapper.findHomeworkSubmissionsByHomeworkId(homeworkId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    @Override
    public List<HomeworkSubmission> getHomeworkSubmissionsByStudentId(Long studentId) {
        try {
            return homeworkSubmissionMapper.findHomeworkSubmissionsByStudentId(studentId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    @Override
    public HomeworkSubmission getHomeworkSubmissionByHomeworkIdAndStudentId(Long homeworkId, Long studentId) {
        try {
            return homeworkSubmissionMapper.findHomeworkSubmissionByHomeworkIdAndStudentId(homeworkId, studentId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public PageInfo<HomeworkSubmission> getAllHomeworkSubmissionsWithPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HomeworkSubmission> homeworkSubmissions = homeworkSubmissionMapper.findAllHomeworkSubmissionsWithPage();
        return new PageInfo<>(homeworkSubmissions);
    }

    @Override
    public PageInfo<HomeworkSubmission> getHomeworkSubmissionsByConditionWithPage(HomeworkSubmission homeworkSubmission, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HomeworkSubmission> homeworkSubmissions = homeworkSubmissionMapper.findHomeworkSubmissionsByCondition(homeworkSubmission);
        return new PageInfo<>(homeworkSubmissions);
    }

    @Override
    public boolean gradeHomeworkSubmission(Long id, Double score) {
        try {
            return homeworkSubmissionMapper.gradeHomeworkSubmission(id, score) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}